package com.myres.struts2.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;

import com.myres.biz.ShopBiz;
import com.myres.dao.ProductDao;
import com.myres.dao.ShopDao;
import com.myres.model.Product;
import com.myres.model.Shop;
import com.myres.model.User;
import com.myres.service.ShopService;
import com.myres.spring.SpringUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AddProductAction extends ActionSupport {
	private User user;
	private Product product;
	private ProductDao productDao;
	
	//private File icon;
	//private String iconFileName;
	//private String iconFileContentType;
	private String path;
	//public File getIcon() {
	//	return icon;
	//}
	//public void setIcon(File icon) {
	//	this.icon = icon;
	//}
	//public String getIconFileName() {
	//	return iconFileName;
	//}
	//public void setIconFileName(String iconFileName) {
	//	this.iconFileName = iconFileName;
	//}
	//public String getIconFileContentType() {
	//	return iconFileContentType;
	//}
	//public void setIconFileContentType(String iconFileContentType) {
	//	this.iconFileContentType = iconFileContentType;
	//}
	public ProductDao getProductDao() {
		return productDao;
	}
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}

	private ShopService getShopListService;
	
	public ShopService getGetShopListService() {
		return getShopListService;
	}
	public void setGetShopListService(ShopService getShopListService) {
		this.getShopListService = getShopListService;
	}
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	/**
	 * @return
	 */
	public String execute() {
		// TODO Auto-generated method stub
		user = (User) ActionContext.getContext().getSession().get("user");
		Set<Shop> shops =getShopListService.getMyShop(user.getId());
		java.util.Iterator<Shop> iter=shops.iterator();
		Shop shop=null;
		while(iter.hasNext()){
			shop= iter.next();
			break;
		}
		if(shop == null){
			return "no_product";
		}
		//try{
		   // saveFile();
		//}catch(Exception e){
		//	e.printStackTrace();
		//}
		//product.setIcon(path);
		product.setShop(shop);
		productDao.save(product);
		return SUCCESS;
	}
//	private void saveFile() throws IOException{
//		path= ServletActionContext.getServletContext().getRealPath("/"); 
//		File f=new File(path);
//		f=f.getParentFile();
//		System.out.println("root="+path);
//		// TODO Auto-generated method stub
//		  java.io.InputStream is = new java.io.FileInputStream(icon);
//		  path="/upload/res_pro/"+ iconFileName;
//		  String path=f.toString() +"/upload/res_pro/"+ iconFileName;
//	      java.io.OutputStream os = new java.io.FileOutputStream(path);
//	      byte buffer[] = new byte[8192];
//	      int count = 0;
//	      while((count = is.read(buffer)) > 0) {
//	            os.write(buffer, 0, count);
//	      }
//	      os.close();
//	      is.close();
//	}
}